#!/usr/sbin/rsct/perl5/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2000,2002 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)30   1.7   src/rsct/rm/SensorRM/cli/bin/rmsensor.perl, sensorcli, rsct_rpyxh, rpyxht1f3 5/20/02 11:00:47

# Removes an RMC IBM.Sensor resource

use strict;
use locale;
BEGIN
  {
	# this enables us to redirect where it looks for other RSCT files during development
	$::rsctroot = $ENV{'RSCT_ROOT'} || '/usr/sbin/rsct';
	$::rsctpm = "$::rsctroot/pm";
	$::rsctmsgmaps = "$::rsctroot/msgmaps";
  }

use lib $::rsctpm;
use Getopt::Std;
use autouse CT_cli_utils => qw(printIMsg printEMsg);
use Socket;

$main::PROGNAME = 'rmsensor';
$main::MSGCAT = 'sensorcli.cat';
$main::LSMSG = '/usr/sbin/rsct/bin/ctdspmsg';

# For the usage, see sensorcli.msg
sub usage { printIMsg('IMsgRmsensorUsage');  exit (scalar(@_) ? $_[0] : 1); }

# Parse the cmd line args and check them
if (! getopts('an:hvV') ) { &usage; }
if (scalar(@ARGV) < 1 || $::opt_h) { &usage; }
if ($::opt_a && $::opt_n) { &usage; }
if ($::opt_V) { $::opt_v = 1; }

my $selectstr = q/Name IN ('/ . join("','",@ARGV) . q/')/;

if ($::opt_a || defined($::opt_n))
  {
	#todo: support symbolic names for values 2 and 3
	if ($ENV{CT_MANAGEMENT_SCOPE}!=2 && $ENV{CT_MANAGEMENT_SCOPE}!=3) { $ENV{CT_MANAGEMENT_SCOPE}=4; }
  }
else { $ENV{CT_MANAGEMENT_SCOPE}=1; }

if (defined($::opt_n))
  {
	my @nodes = split(/[, ]+/,$::opt_n);
	#&resolve(\@nodes);
	if (length($selectstr)) { $selectstr .= ' && '; }
	$selectstr .= q/NodeNameList IN ('/ . join("','",@nodes) . q/')/;
  }

if (length($selectstr)) { $selectstr = qq("$selectstr"); }

my $cmd = qq(/usr/bin/rmrsrc-api -s IBM.Sensor::$selectstr 2>&1);
if ($::opt_v) { $main::PROGNAME = 'lssensor'; printIMsg('IMsgRmcCmd', $cmd); $main::PROGNAME = 'rmsensor'; }

my @output = `$cmd`;
my $rc = $? >> 8;
if ($rc)
  {
	if (!$::opt_v) { $output[0] =~ s/.*::.*::.*::.*::.*:://; }
	print @output;
	exit $rc;
  }

exit $rc;


# Not used right now
sub resolve
  {
	my ($arrayref) = @_;
	foreach my $host (@$arrayref)
	  {
		if ($host =~ /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/)     # an ip address
		  {
			my $packedaddr = inet_aton($host);
			my $hostname = gethostbyaddr($packedaddr, AF_INET);
			if (length($hostname)) { $host = $hostname; }
		  }
		else					# they specified a hostname, but it may be a short name
		  {
			my ($hostname, $aliases, $addrtype, $length, @addrs) = gethostbyname($host);
			if (length($hostname)) { $host = $hostname; }
		  }
	  }
  }
